<template>
  <div>
    <div class="content">
      <div class="return" @click="close">
        <img class="img1" src="../../assets/img/return.png" alt="" />
        <p class="return-p">返回</p>
      </div>
      <div class="card1" v-for="(item, index) in list" :key="index">
        <div class="card1-text">助理账号：{{ item.phone }}</div>
        <div class="card1-text">创建时间：{{ item.time }}</div>
        <div class="card1-text">账号状态：{{ item.state == 1 ? '正常' : '禁用' }}</div>
        <div class="card1-text">修改状态：<div class="editBtns" v-if="item.state == 1" @click="forbid(item,2)">禁用</div> <div v-else class="editBtn" @click="forbid(item,1)">启用</div></div>
      </div>
      <div class="norecords" v-if="list && list.length == 0">
        暂无记录
      </div>
      <div class="addStore" @click="addMember">
        新增助理
      </div>

    </div>
    <van-dialog v-model="isSureShow" title="" :show-cancel-button="false" :showConfirmButton="false" width="80%">
      <div style="padding: 0 0.5rem 0.5rem">
        <div class="tipsContent">
          <div class="addAssistant">新增助理账号</div>
          <input class="input" type="number" v-model="phone" placeholder="请输入助理手机号" />
          <div class="line"></div>
          <input class="input" type="number" v-model="pwd" placeholder="请输入助理密码" />
          <div class="line"></div>
        </div>
        <div style="
              display: flex;
              align-items: center;
              justify-content: space-around;
            ">
          <button class="cancelBtn" @click="cancel">
            <span>取消</span>
          </button>
          <button class="confirmBtn" @click="sureFn">
            <van-loading v-if="loadings" size="24px">新增中...</van-loading>
            <span v-else>新增</span>
          </button>
        </div>
      </div>
    </van-dialog>
  </div>
</template>
  
<script>
import { getStaff, addStaff, manageStaff } from "@/api/fulfil.js";
export default {
  name:'assistant',
  data() {
    return {
      agentid: "",
      list: [],
      role: "",
      show: true,
      isSureShow: false,
      phone:'',
      pwd:'',
      loadings:false,
      
    };
  },
  mounted() {
    
  },
  activated() {
    this.role = localStorage.getItem("role");
    this.getIndex();
  },
  methods: {
    getIndex() {
      getStaff({
        agentid: localStorage.getItem("agentid"),
      })
        .then((res) => {
          this.list = res.data.data;
          console.log(res);
        })
        .catch((error) => {
          console.log(error);
        });
    },
    //禁用启用
    manage(item,type) {
      manageStaff({
        agentid: localStorage.getItem("agentid"),
        staffid:item.id,
        state:type.toString() //1 可用 2 禁用
      })
        .then((res) => {
          if(res.data.code == 0){
            if(type == 1){
              this.$Toast("启用助理账号成功");
            }else{
              this.$Toast("禁用助理账号成功");
            }
            this.getIndex();
          }else{
            this.$Toast("修改状态失败");
          }
          console.log(res);
        })
        .catch((error) => {
          console.log(error);
        });
    },
    forbid(item,type){
      this.manage(item,type);
    },
    //新增助理
    addStaff() {
      addStaff({
        agentid: localStorage.getItem("agentid"),
        phone: this.phone,
        pwd: this.pwd
      }).then((res) => {
        console.log(res);
        if(res.data.code == 0){
          this.$Toast("新增助理成功");
          this.isSureShow = false;
          this.getIndex();
        }else{
          this.$Toast(res.data.msg)
        }
        this.loadings = false;
      }).catch((error) => {
        console.log(error);
        this.loadings = false;
      });
    },
    cancel() {
      this.isSureShow = false;
    },
    sureFn() {
      if (!this.phone) {
        this.$Toast("新增助理手机号不能为空");
      } else if (!this.pwd) {
        this.$Toast("新增助理密码不能为空");
      } else {
        this.loadings = true;
        this.addStaff();
      }
    },
    close() {
      window.history.go(-1);
    },
    //新增助理
    addMember() {
      this.isSureShow = true;
    }
  },
};
</script>
  
<style scoped>
.content {
  padding-bottom: 30px;
  overflow: hidden;
  width: 100%;
  min-height: 100vh;
  background: #e1f7fc;
}

.card1 {
  overflow: hidden;
  margin: 0.3rem auto 0;
  padding: 0 0 0.25rem 0.5rem;
  width: 75%;
  background: #fff;
  border-radius: 0.375rem;
}

.card1-title {
  margin-top: 0.2rem;
  font-size: 0.4125rem;
  font-weight: 600;
  color: #000;
}

.card1-text {
  margin: 0.25rem 0 0 0.2rem;
  font-size: 0.3125rem;
  font-weight: 600;
  color: #000;
}

.card1-text1 {
  margin: 0.25rem 0 0 0.2rem;
  font-size: 0.3125rem;
  font-weight: 600;
  color: #000;
  text-align: center;
}

.img1 {
  display: inline-block;
  width: 27px;
  height: 48px;
  vertical-align: middle;
}

.return {
  margin: 20px 0 0 30px;
  font-size: 40px;
}

.return-p {
  margin-left: 20px;
  display: inline-block;
  vertical-align: middle;
}

.norecords {
  margin-top: 2rem;
  color: #ffbe4c;
  font-size: .5rem;
  text-align: center;
}

.addStore {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1rem;
  font-size: .4rem;
  text-align: center;
  color: #AD3516;
  background: linear-gradient(180deg, #FFE9C6, #FFD584);
  box-shadow: 0px 3px 8px 0px rgba(150, 51, 20, 0.42);
  line-height: 1rem;
  border-radius: .5rem;
}

.confirmBtn {
  margin-top: 0.5rem;
  width: 3rem;
  height: 1rem;
  background: linear-gradient(315deg, #4441ee 0%, #920ccc 100%);
  box-shadow: 0rpx 4rpx 16rpx 0rpx rgba(0, 0, 0, 0.25);
  border-radius: 0.6rem;
  opacity: 1;
  color: #ffffff;
  border: none;
  font-size: 0.4rem;
}

.cancelBtn {
  margin-top: 0.5rem;
  width: 3rem;
  height: 1rem;
  background: #e2c7ff;
  box-shadow: 0rpx 4rpx 16rpx 0rpx rgba(0, 0, 0, 0.25);
  border-radius: 0.6rem;
  opacity: 1;
  color: #6533d2;
  border: none;
  font-size: 0.4rem;
}

.tipsContent {
  width: 100%;
  margin: 0.5rem 0 .3rem;
  display: inline-block;
  font-size: 0.4rem;
  font-family: PingFang SC-Medium, PingFang SC;
  font-weight: 500;
  color: #111111;
}

.input {
  margin-left: 0.1rem;
  margin-top: 0.4rem;
  width: 60%;
  background: white;
  border: none;
  padding: 0.1875rem 0.25rem;
  border-radius: 0.25rem;
  /*  border-inline: 2px solid transparent; */
}

.addAssistant {
  width: 100%;
  text-align: center;
  font-size: .4rem;
}

.line {
  margin: 0 auto;
  width: 90%;
  height: 2px;
  background-color: #ccc;
}
.editBtn {
  padding:0 .4rem;
  display: inline-block;
  background-color: #FFD584;
  border-radius: .4rem;
  height: .5rem;
  text-align: center;
  line-height: .5rem;
}
.editBtns {
  padding:0 .3rem;
  display: inline-block;
  background-color: red;
  border-radius: .4rem;
  height: .5rem;
  text-align: center;
  line-height: .5rem;
  color: #fff;
}
</style>